<?php

declare(strict_types=1);

namespace app\index\controller;

use addons\OssBackup\facade\OssBackup;
use app\common\model\Logs;
use app\common\model\Statistics;
use app\common\tool\Result;

class Callback
{

    /**
     * @return void
     * 自动备份
     */
    public function autoOssBackup()
    {
        try {
            $route = 'callback/autoOssBackup.log';
            $result = OssBackup::auto();
            Logs::writeLog($route, json_encode($result));
        } catch (\Exception $e) {
            Logs::writeLog($route, $e->getMessage());
        }
    }

    /**
     * @return void
     * 更新统计
     */
    public function statistics()
    {
        Statistics::_timing();
    }
    /**
     * 定时
     */
    public function timing()
    {
        $param = request()->get();
        $type = $param['type'];
        $route = 'callback/' . $type . '.log';
        if (!in_array($type, ['release', 'repealOrder', 'waitRelease', 'autoRevokelOrder', 'weekRevokelOrder'])) {
            Logs::writeLog($route, '参数错误');
            return Result::fail('参数错误');
        }
        try {
            switch ($type) {
                case 'release':
                    //通证释放
                    $res = \app\common\model\User::release();
                    break;
                case 'repealOrder':
                    // 未付款撤销
                    $res = \app\common\model\DealOrder::repealOrder();
                    break;
                case 'waitRelease':
                    //待出售积分释放
                    $res = \app\common\model\DealRelease::waitRelease();
                    break;
                case 'autoRevokelOrder':
                    //自动撤单
                    $res = \app\common\model\DealOrder::autoRevokelOrder();
                    break;
                case 'weekRevokelOrder':
                    //周撤单
                    $res = \app\common\model\DealOrder::weekRevokelOrder();
                    break;
                default:
                    return ['code' => -1, 'msg' => '参数错误'];
                    break;
            }
            if ($res['code'] == 1) {
                Logs::writeLog($route, json_encode($res));
                return Result::success( '定时任务执行成功');
            } else {
               return Result::fail($res['msg']);
            }
        } catch (\Exception $e) {
            Logs::writeLog($route, $e->getMessage());
            return Result::fail($e->getMessage());
        }
    }
    
    /**
     * 锁仓释放
     */
    public function lock_release()
    {
        try {
            $param = request()->get();
            $info = \app\common\model\DealOrder::where(['id'=>$param['id'],'status'=>3,'shifang'=>0])->find();
            if (!$info) return Result::fail('参数错误');
            $muser = \app\common\model\User::where(['id' => $info->buy_uid])->find();
            return Result::fastReturn(\app\common\model\Relation::lock_reward($info, $muser));
        }catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /***
     * 
     */
    public function myrepealOrder()
    {
        try {
            $param = request()->get();
            return Result::fastReturn(\app\common\model\DealOrder::myrepealOrder($param['id']));
        }catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    public function alipayCallback()
    {
        try {
            $route = 'callback/alipayCallback.log';
            $pay = \AliPay\App::instance(config('pay')['alipay']);
            $data = $pay->notify();
            if (in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
                // @todo 更新订单状态，支付完成

                Logs::write($route, '订单支付成功');
                return 'SUCCESS';
            } else {
                // @todo 回调验证失败处理
                Logs::write($route, '回调验证失败:' . json_encode($data));
            }
        } catch (\Exception $e) {
            // @todo 异常处理
            Logs::write($route, $e->getMessage());
        }
    }


    public function wechatCallback()
    {
        try {
            $route = 'callback/wechatCallback.log';
            $wechat = \WeChat\Pay::instance(config('pay')['wechat']);
            $data = $wechat->getNotify();
            if ($data['return_code'] === 'SUCCESS' && $data['result_code'] === 'SUCCESS') {
                // @todo 更新订单状态，支付完成


                // 返回接收成功的回复
                Logs::write($route, '订单支付成功');
                return $wechat->getNotifySuccessReply();
            } else {
                // @todo 回调验证失败处理
                Logs::write($route, '回调验证失败：' . json_encode($data));
            }
        } catch (\Exception $e) {
            // @todo 异常处理
            Logs::write($route, $e->getMessage());
        }
    }
    /**
     * 回退锁仓
     */
    public function return_lock()
    {
        return Result::fastReturn(\app\common\model\DealRelease::return_lock());
    }
    
}
